import SwiftUI

// LookDetail
// 看一看详情

struct LookDetail: View {
    @State private var showAddTrade: Bool = false
    
    @StateObject private var userInfoFetcher = LoginUserInfoFetcher()
    
    var book: Book
    
    var body: some View {
        ScrollView {
            VStack(alignment: .leading, spacing: 10) {
                Text(book.tag)
                    .font(.title3)
                    .fontWeight(.light)
                    .foregroundColor(.accent)
                
                Text(book.bookName)
                    .font(.title)
                    .fontWeight(.light)
                
                Text(String(format: "¥%.2f", Double(book.requirePrice) / 100))
                    .font(.title2)
                    .fontWeight(.light)
                    .foregroundColor(.orange)
                
                Button {
                    withAnimation {
                        showAddTrade.toggle()
                    }
                } label: {
                    Spacer()
                    Text("转起来")
                        .foregroundColor(.white)
                        .font(.title3)
                        .padding(.vertical, 10)
                    Spacer()
                }
                .background(.accent)
                .cornerRadius(8)
                
                Divider()
                
                HStack {
                    Text("出售者：")
                        .font(.subheadline)
                        .foregroundColor(.accent)
                    Text((userInfoFetcher.publishedResult?.realStudentNumber ?? "") + (userInfoFetcher.publishedResult?.realName ?? ""))
                        .font(.subheadline)
                        .foregroundColor(.accent)
                }
                
                Text("联系方式")
                    .font(.subheadline)
                    .foregroundColor(.accent)
                
                Text(userInfoFetcher.publishedResult?.contact ?? "")
                    .font(.subheadline)
                
                Divider()
                
                Text("作者：" + book.author)
                    .font(.subheadline)
                     
                Text("课程：" + book.course)
                    .font(.subheadline)

                Divider()
                
                Text("简介")
                    .font(.title2)
                
                Text(book.description)
                
                Divider()
                
                Text("图片")
                    .font(.title2)
                
                LazyVGrid(columns: Array(repeating: GridItem(.flexible()), count: 3)) {
                    ForEach(book.imageId, id:\.self) { imageId in
                        let imageURL = URL(string: "http://62.234.45.120:1230/storage/bookImage/" + imageId + ".jpeg")!
                        GeometryReader { geo in
                            NavigationLink(destination:PhotoDetail(imageURL: imageURL)) {
                                AsyncImage(url: imageURL) {image in
                                    image
                                        .resizable()
                                } placeholder: {
                                    ProgressView()
                                }
                            }
                        }
                        .cornerRadius(8.0)
                        .aspectRatio(1, contentMode: .fit)
                    }
                }
            }
            .sheet(isPresented: $showAddTrade) {
                AddTradePage(book: book)
            }
            .padding()
            .task {
                userInfoFetcher.fetch(userId: book.userId)
            }
            .navigationTitle("详情")
            .navigationBarTitleDisplayMode(.inline)
        }
    }
}

#Preview {
    LookDetail(book: DefaultBook)
}
